package cp5_jdbc.user_test;

import util.Constants;

import java.sql.*;
import java.util.Scanner;

public class UserLogin {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 查看所有的驱动
        // 2. 获取数据库连接
        Connection conn = DriverManager.getConnection(Constants.URL, Constants.USERNAME, Constants.PASSWORD);
        Scanner input = new Scanner(System.in);
        System.out.printf("请输入用户名：");
        String name = input.next();
        System.out.printf("请输入密码:");
        String pass = input.next();
        String sql = "select * from userinfo where account='" + name + "' and password='" + pass + "' limit 1";
        System.out.println(sql);
        // 创建命令
        Statement st = conn.createStatement();
        // 执行sql获取结果
        ResultSet rs = st.executeQuery(sql);
        // 判断是否有数据
        if(rs.next()){
            String nickname = rs.getString("nickname");
            System.out.println("登录成功，欢迎 " + nickname);
        }else{
            System.out.println("用户名或者密码错误");
        }
        // 关闭
        rs.close();
        st.close();
        conn.close();
    }
}
